package co.yixiang.modules.share.rest;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.hutool.http.HttpUtil;
import co.yixiang.api.ApiResult;
import co.yixiang.api.YshopException;
import co.yixiang.common.bean.LocalUser;
import co.yixiang.common.interceptor.AuthCheck;
import co.yixiang.enums.CouponGetEnum;
import co.yixiang.modules.activity.domain.YxStoreCouponUser;
import co.yixiang.modules.activity.domain.YxStoreCouponUserQrTemp;
import co.yixiang.modules.activity.service.YxStoreCouponUserService;
import co.yixiang.modules.auth.param.LoginParam;
import co.yixiang.modules.coupon.rest.CouponController;
import co.yixiang.modules.mp.config.WxMaConfiguration;
import co.yixiang.utils.StringUtils;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(value = "小程序分享优惠券模块", tags = "小程序分享优惠券模块")
public class shareController {

    @AuthCheck
    @PostMapping("/wxapp/share")
    @ApiOperation(value = "小程序分享", notes = "小程序分享")
    public ApiResult<String> share(@RequestBody Map param, HttpServletRequest request) throws WxErrorException {
        Object id = param.get("id");

        WxMaService wxMaService = WxMaConfiguration.getWxMaService();
        String accessToken = wxMaService.getAccessToken();
        Map<String,Object> map= new HashMap<>();

        Map<String,String> map2= new HashMap<>();
        map2.put("path","/pages/user/User/receiveCoupon");
        //map2.put("path","/pages/home/index");
        map2.put("query","id="+id);

        map.put("jump_wxa",map2);

        //最长有效期2天
        map.put("expire_interval",2);
        map.put("expire_type",1);



        String post = HttpUtil.post("https://api.weixin.qq.com/wxa/generatescheme?access_token=" + accessToken, JSON.toJSONString(map));
        //"weixin://dl/business/?t=d7LBkxx0Eju";
        log.info(post,"小程序分享链接回调");
        return ApiResult.ok(post);
    }


    @Autowired
    private CouponController couponController;

    @Autowired
    private YxStoreCouponUserService storeCouponUserService;

    @AuthCheck
    @PostMapping("/wxapp/receive")
    @ApiOperation(value = "小程序分享-领取", notes = "小程序分享-领取")
    public ApiResult receive(@RequestBody Map param, HttpServletRequest request)  {

        Long uid = LocalUser.getUser().getUid();


        String id = (String)param.get("id");
        YxStoreCouponUserQrTemp tempQr = couponController.getTempQr(id);
        YxStoreCouponUser couponUserOne = storeCouponUserService.getById(tempQr.getCouponUserId());
        if (couponUserOne.getPUid()==null){
            Long oldUid = couponUserOne.getUid();
            couponUserOne.setUid(uid);
            couponUserOne.setPUid(oldUid);
            couponUserOne.setType(CouponGetEnum.SHARE.getValue());
            //couponUserOne.setUpdateTime(new Date());
            storeCouponUserService.updateById(couponUserOne);
        }else{
            throw new YshopException("此优惠券已被领取");
        }


        return ApiResult.ok();
    }

}
